package com.lww.feature.util;


import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import javax.mail.util.ByteArrayDataSource;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * @author yi an
 * @date 2017/3/29
 */
public class EmailUtil {

    public static void sendEmailAttach(InputStream is, List<String> toEmails, String subject,String fileName) throws MessagingException, IOException {

        String smtpFromMail = "yian@xiaokakeji.com";  //账号
        String pwd = "×××××××××"; //密码
        String host = "smtp.exmail.qq.com"; //端口
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true");
        Session session = Session.getDefaultInstance(props);
        session.setDebug(false);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(smtpFromMail, "settle"));
        Address[] addresses = new Address[toEmails.size()];
        for (int i =0;i<toEmails.size();i++) {
            addresses[i] = new InternetAddress(toEmails.get(i));
        }
        message.addRecipients(Message.RecipientType.TO, addresses);
        message.setSubject(subject);
        message.addHeader("charset", "UTF-8");
        /*添加正文内容*/
        Multipart multipart = new MimeMultipart();
        BodyPart contentPart = new MimeBodyPart();
        contentPart.setText("内容");
        contentPart.setHeader("Content-Type", "text/html; charset=UTF-8");
        multipart.addBodyPart(contentPart);
        /*添加附件*/
        MimeBodyPart fileBody = new MimeBodyPart();
        DataSource source = new ByteArrayDataSource(is, "text/plain");
        fileBody.setDataHandler(new DataHandler(source));
        fileBody.setFileName(MimeUtility.encodeText(fileName,"GBK",null));
        multipart.addBodyPart(fileBody);
        message.setContent(multipart);
        message.setSentDate(new Date());
        message.saveChanges();
        Transport transport = session.getTransport("smtp");
        transport.connect(host, smtpFromMail, pwd);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
    }

    //下面为spring的方式发邮件
//            JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
//        //设定mail server
//        senderImpl.setHost("smtp.exmail.qq.com");
//        //建立邮件消息,发送简单邮件和html邮件的区别
//        MimeMessage mailMessage = senderImpl.createMimeMessage();
//        //注意这里的boolean,等于真的时候才能嵌套图片，在构建MimeMessageHelper时候，所给定的值是true表示启用，
//        //multipart模式 为true时发送附件 可以设置html格式
//        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
//
//        //设置收件人，寄件人
//        messageHelper.setTo("243242811@qq.com");
//        messageHelper.setFrom("yian@xiaokakeji.com");
//        messageHelper.setSubject("测试邮件中上传附件!！");
//        //true 表示启动HTML格式的邮件
//        messageHelper.setText("请查看附件！");
//
//        //这里的方法调用和插入图片是不同的。
//        File file = new File("d:/test.csv");
//        InputStream in =  new FileInputStream(file);
//        InputStreamResource inputStreamResource = new InputStreamResource(in);
//        messageHelper.addAttachment("搜索.csv",inputStreamResource);
//
////        senderImpl.setUsername("username") ; // 根据自己的情况,设置username
////        senderImpl.setPassword("password") ; // 根据自己的情况, 设置password
//        Properties prop = new Properties() ;
//        prop.put("mail.smtp.auth", "false") ; // 将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确
//        prop.put("mail.smtp.timeout", "25000") ;
//        senderImpl.setJavaMailProperties(prop);
//        //发送邮件
//        senderImpl.send(mailMessage);
//
//        System.out.println("邮件发送成功..");


